package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private FriendApi friendApi;

    /**
     * 探花 - 右滑 喜欢
     *
     * @param loginUserId
     * @param userId
     * @return
     */
    @Override
    public Boolean love(Long loginUserId, Long userId) {
        //1. 判断是否存在记录(条件userId=loginUserId, likeUserId=userId)
        Query query = new Query(Criteria.where("userId").is(loginUserId).and("likeUserId").is(userId));
        long timeMillis = System.currentTimeMillis();
        if (mongoTemplate.exists(query, UserLike.class)) {
            //2. 存在则更新
            Update update = new Update();
            update.set("isLike", true);
            update.set("updated", timeMillis);
            mongoTemplate.updateFirst(query, update, UserLike.class);
        }else{
            //3. 不存在则添加
            UserLike userLike = new UserLike();
            userLike.setUserId(loginUserId);
            userLike.setLikeUserId(userId);
            userLike.setCreated(timeMillis);
            userLike.setUpdated(timeMillis);
            userLike.setIsLike(true);
            mongoTemplate.insert(userLike);
        }
        //4. 判断对应是否也喜欢登录用户(条件userId=userId, likeUserId=loginUserId, isLike=true)
        query = new Query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(loginUserId)
                .and("isLike").is(true));
        if (mongoTemplate.exists(query, UserLike.class)) {
            //5. 调用friendApi添加好友, 返回 true
            friendApi.makeFriends(loginUserId, userId);
            return true;
        }
        //6. 返回false, 不是好友
        return false;
    }

    /**
     * 探花 - 左滑 不喜欢
     *
     */
    @Override
    public void unLove(Long loginUserId, Long userId) {
        //1. 判断是否存在记录(条件userId=loginUserId, likeUserId=userId)
        Query query = new Query(Criteria.where("userId").is(loginUserId).and("likeUserId").is(userId));
        long timeMillis = System.currentTimeMillis();
        if (mongoTemplate.exists(query, UserLike.class)) {
            //2. 存在则更新
            Update update = new Update();
            update.set("isLike", false);
            update.set("updated", timeMillis);
            mongoTemplate.updateFirst(query, update, UserLike.class);
        }else{
            //3. 不存在则添加
            UserLike userLike = new UserLike();
            userLike.setUserId(loginUserId);
            userLike.setLikeUserId(userId);
            userLike.setCreated(timeMillis);
            userLike.setUpdated(timeMillis);
            userLike.setIsLike(false);
            mongoTemplate.insert(userLike);
        }
    }
}
